package com.sam.jifenbao;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sam.entity.PointsRedeemEntity;
import com.sam.jifenbao.dialog.RedeemDialog;
import com.sam.widget.ExpandListView;
import com.sam.widget.headerBar.BaseHeaderView;

import java.util.ArrayList;
import java.util.List;

public class PointsRedeemActivity extends BaseHeaderActivity {

    private ExpandListView pointsRedeemListView;
    private ImageView userPortrait;
    private TextView userNameTV,userPointsTV,couponCountsTV;
    private View pointsBtn,couponBtn;
    private Button walletBtn;

    private long userPoints = 2000;
    private int couponCount = 5;

    private ScrollView scrollView;

    private PointsRedeemAdapter pointsRedeemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_points_redeem);
        findView();
        initView();
    }


    @Override
    protected void findView() {
        pointsRedeemListView = (ExpandListView) findViewById(R.id.points_redeem_listview);
        userPortrait = (ImageView) findViewById(R.id.user_portrait);
        userNameTV = (TextView) findViewById(R.id.user_name);
        userPointsTV = (TextView) findViewById(R.id.user_points);
        couponCountsTV = (TextView) findViewById(R.id.coupon_count);
        walletBtn = (Button) findViewById(R.id.wallet_btn);
        pointsBtn = findViewById(R.id.points_btn);
        couponBtn = findViewById(R.id.coupon_btn);
        scrollView = (ScrollView) findViewById(R.id.scroll_view);
    }

    @Override
    protected void initView() {
        getHeaderView().setTitleText(R.string.test_user_name);
        userPortrait.setOnClickListener(onClickListener);
        walletBtn.setOnClickListener(onClickListener);
        couponBtn.setOnClickListener(onClickListener);
        pointsBtn.setOnClickListener(onClickListener);

        List<PointsRedeemEntity> pointsRedeemEntityList = new ArrayList<>();
        pointsRedeemEntityList.add(new PointsRedeemEntity("和风沙拉酱一万瓶",1000));
        pointsRedeemEntityList.add(new PointsRedeemEntity("旺仔牛奶一罐",500));
        pointsRedeemEntityList.add(new PointsRedeemEntity("掉漆茶杯",2000));
        pointsRedeemEntityList.add(new PointsRedeemEntity("快用完的餐巾纸",1000));
        pointsRedeemEntityList.add(new PointsRedeemEntity("长得很丑的茶杯",200));
        pointsRedeemEntityList.add(new PointsRedeemEntity("Sam的板砖",100000));
        pointsRedeemEntityList.add(new PointsRedeemEntity("得力标记笔",50));
        pointsRedeemEntityList.add(new PointsRedeemEntity("美的快报废的空调一台",10000));
        pointsRedeemEntityList.add(new PointsRedeemEntity("Sam的茶杯",99999));
        pointsRedeemEntityList.add(new PointsRedeemEntity("卡到不行的电脑",100));
        pointsRedeemEntityList.add(new PointsRedeemEntity("喝了一半的农夫山泉",2));
        pointsRedeemEntityList.add(new PointsRedeemEntity("价值35块钱的要含着才能用的麦克风",3500));

        pointsRedeemAdapter = new PointsRedeemAdapter(this,pointsRedeemEntityList);
        pointsRedeemListView.setAdapter(pointsRedeemAdapter);
        updateUserPoints();
        updateCouponCount();
//        scrollView.smoothScrollTo(0,0);


//        scrollView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                scrollView.scrollTo(0,0);
//            }
//        },5000);

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.user_portrait:
                    startActivity(new Intent(PointsRedeemActivity.this,UserCenterActivity.class));
                    break;
                case R.id.wallet_btn:
                    startActivity(new Intent(PointsRedeemActivity.this,MyWalletActivity.class));
                    break;
                case R.id.coupon_btn:
                    startActivity(new Intent(PointsRedeemActivity.this,CouponListActivity.class));
                    break;
                case R.id.points_btn:
                    startActivity(new Intent(PointsRedeemActivity.this,PointsRecordActivity.class));
                    break;
            }
        }
    };

    private void updateUserPoints(){
        userPointsTV.setText(userPoints+"");
        pointsRedeemAdapter.notifyDataSetChanged();
    }

    private void updateCouponCount(){
        couponCountsTV.setText(couponCount+"");
    }



    private class PointsRedeemAdapter extends BaseAdapter{
        private Context context;
        private List<PointsRedeemEntity> pointsRedeemList;

        public PointsRedeemAdapter(Context context, List<PointsRedeemEntity> pointsRedeemList){
            this.context = context;
            this.pointsRedeemList = pointsRedeemList;
        }

        @Override
        public int getCount() {
            return pointsRedeemList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHold viewHold;
            if(convertView == null){
                convertView = View.inflate(context,R.layout.redeem_points_list,null);
                viewHold = new ViewHold();
                viewHold.pointsRedeemNameTV = (TextView) convertView.findViewById(R.id.points_redeem_name);
                viewHold.redeemPointsTV = (TextView) convertView.findViewById(R.id.redeem_points);
                viewHold.redeemButton = (Button) convertView.findViewById(R.id.redeeem_button);
                convertView.setTag(viewHold);
            } else {
                viewHold = (ViewHold) convertView.getTag();
            }
            final PointsRedeemEntity pointsRedeemEntity = pointsRedeemList.get(position);
            viewHold.pointsRedeemNameTV.setText(pointsRedeemEntity.getRedeemName());
            viewHold.redeemPointsTV.setText(pointsRedeemEntity.getRedeemPoints()+"");
            if(userPoints >= pointsRedeemEntity.getRedeemPoints()){
                viewHold.redeemButton.setEnabled(true);
                viewHold.redeemButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RedeemDialog redeemDialog = new RedeemDialog(context,pointsRedeemEntity.getRedeemPoints(),pointsRedeemEntity.getRedeemName());
                        redeemDialog.setOnRedeemSccueededLinstener(new RedeemDialog.OnRedeemSccueededLinstener() {
                            @Override
                            public void onRedeemSeccueeded() {
                                userPoints = userPoints - pointsRedeemEntity.getRedeemPoints();
                                updateUserPoints();
                            }
                        });
                        redeemDialog.show();
                    }
                });
            } else {
                viewHold.redeemButton.setEnabled(false);
            }

            return convertView;
        }

        class ViewHold {
            TextView pointsRedeemNameTV,redeemPointsTV;
            Button redeemButton;
        }
    }
}
